<template>
	<view class="mord">
		<view class="top">
			<view @click='tab(0)' :class="{'active':num==0}">
				全部
			</view>
			<view @click='tab(1)' :class="{'active':num==1}">
				待审批
			</view>
			<view @click='tab(2)' :class="{'active':num==2}">
				待退货
			</view>
			<view @click='tab(3)' :class="{'active':num==3}">
				待退款
			</view>
			<view @click='tab(4)' :class="{'active':num==4}">
				已完成
			</view>
			<view @click='tab(5)' :class="{'active':num==5}">
				已驳回
			</view>
			<view @click='tab(6)' :class="{'active':num==6}">
				已取消
			</view>
		</view>
		<scroll-view @scrolltolower='sc' scroll-y="true" class="ddlist" lower-threshold="50">

			<view class="li" v-for="l in data" :key='l.id'>
				<view class="li-up">
					服务单号:{{l.cSaleReturnNo}}
					<view class="r">
						{{l.cStatusName}}
					</view>
				</view>
				<navigator v-for='ls in l.oSaleReturnDetails' v-if="l.cSaleReturnStatus == 'SUBMITSALERETURN'"
					:url="'/pages/user/returnDetailById?cSaleReturnNo='+l.cSaleReturnNo" class="con">
					<view class="img">
						<image :src="ls.oProductAlbum.imgUrl" mode="widthFix"></image>
					</view>
					<view class="text">
						{{ls.cProductName}}

					</view>
					<view class="numb">
						￥{{ls.fSalePrice}}

					</view>

				</navigator>
				<navigator v-for='ls in l.oSaleReturnDetails' v-if="l.cSaleReturnStatus == 'CONFIRMSALERETURNORDER'"
					:url="'/pages/user/returnDetailById?cSaleReturnNo='+l.cSaleReturnNo" class="con">
					<view class="img">
						<image :src="ls.oProductAlbum.imgUrl" mode="widthFix"></image>
					</view>
					<view class="text">
						{{ls.cProductName}}

					</view>
					<view class="numb">
						￥{{ls.fSalePrice}}
						<view>
							x{{ls.iQuantity}}
						</view>
					</view>

				</navigator>

				<navigator v-for='ls in l.oSaleReturnDetails' v-if="l.cSaleReturnStatus == 'BUYERRETURN'"
					:url="'/pages/user/returnDetailById?cSaleReturnNo='+l.cSaleReturnNo" class="con">
					<view class="img">
						<image :src="ls.oProductAlbum.imgUrl" mode="widthFix"></image>
					</view>
					<view class="text">
						{{ls.cProductName}}

					</view>
					<view class="numb">
						￥{{ls.fSalePrice}}
					</view>

				</navigator>
				<navigator v-for='ls in l.oSaleReturnDetails' v-if="l.cSaleReturnStatus == 'CANCELSALERETURN'"
					:url="'/pages/user/returnDetailById?cSaleReturnNo='+l.cSaleReturnNo" class="con">
					<view class="img">
						<image :src="ls.oProductAlbum.imgUrl" mode="widthFix"></image>
					</view>
					<view class="text">
						{{ls.cProductName}}

					</view>
					<view class="numb">
						￥{{ls.fSalePrice}}
					</view>

				</navigator>
				<navigator v-for='ls in l.oSaleReturnDetails' v-if="l.cSaleReturnStatus == 'ENDSALERETURN'"
					:url="'/pages/user/returnDetailById?cSaleReturnNo='+l.cSaleReturnNo" class="con">
					<view class="img">
						<image :src="ls.oProductAlbum.imgUrl" mode="widthFix"></image>
					</view>
					<view class="text">
						{{ls.cProductName}}

					</view>
					<view class="numb">
						￥{{ls.fSalePrice}}
					</view>

				</navigator>
				<navigator v-for='ls in l.oSaleReturnDetails' v-if="l.cSaleReturnStatus == 'OPPOSESALERETURN'"
					:url="'/pages/user/returnDetailById?cSaleReturnNo='+l.cSaleReturnNo" class="con">
					<view class="img">
						<image :src="ls.oProductAlbum.imgUrl" mode="widthFix"></image>
					</view>
					<view class="text">
						{{ls.cProductName}}

					</view>
					<view class="numb">
						￥{{ls.fSalePrice}}
					</view>

				</navigator>
				<view class="butbox">
					<view class="butb" @click="out(l.cSaleReturnNo)"
						v-if="l.cSaleReturnStatus == 'SUBMITSALERETURN' || l.cSaleReturnStatus =='BUYERRETURN' || l.cSaleReturnStatus == 'CONFIRMSALERETURNORDER'">
						取消退款
					</view>
					<view @click="link(l)" class="buta">
						查看详情
					</view>
					<!-- <view v-if="l.cNextStatus == 'DELIVERGOODS'" class="buta">
						提醒发货
					</view> -->
					<view @click='modify(l)'
						v-if="l.cSaleReturnStatus == 'SUBMITSALERETURN' || l.cSaleReturnStatus =='BUYERRETURN' || l.cSaleReturnStatus == 'CONFIRMSALERETURNORDER'"
						class="buta">
						修改
					</view>
					<view @click='oderNew(l.oSaleReturnDetails)'
						v-if="l.cNextStatus ==  'ENDORDER' || l.cNextStatus == 'CLOSEORDER'" class="buta">
						再次购买
					</view>
					<navigator v-if="l.cNextStatus == 'TAKEDELIVERY'" :url="'/pages/user/logistics?id='+l.cOrderNo"
						class="buta">
						查看物流
					</navigator>
					<view @click="shou(l.cOrderNo)" v-if="l.cNextStatus == 'TAKEDELIVERY'" class="butb">
						确认收货
					</view>
					<!-- <view @click='bach(l.oSaleReturnDetails[0].id,l.oSaleReturnDetails[0].iProductId,l.id,l.oSaleReturnDetails[0].iSKUId,l.oSaleReturnDetails[0].cOrderNo,l.oSaleReturnDetails[0].cProductName,l.oSaleReturnDetails[0].DefaultImage)' v-if="l.cNextStatus == 'ENDORDER'" class="buta"> -->
					<view @click='bach(l.oSaleReturnDetails,l.id)'
						v-if="l.cNextStatus == 'ENDORDER' && l.oSaleReturnDetails[0].iCommentId<=0" class="buta">
						评论
					</view>
					<navigator :url="'/pages/user/odersh?cOrderNo='+l.cOrderNo"
						v-if="l.cNextStatus == 'ENDORDER'|| l.cNextStatus =='DELIVERGOODS'" class="buta">
						申请售后
					</navigator>
					<view @click='del(l.id)' v-if="l.cNextStatus == 'ENDORDER' || l.cNextStatus == 'CLOSEORDER'"
						class="butb">
						删除订单
					</view>
				</view>
			</view>

		</scroll-view>
		<view class="meng" v-if="meng == true">
			<view class="tan">
				<view class="titles">
					选择取消原因
					<view class="closes" @click="closes">
						<image src="../../static/img/1/30.png" mode="widthFix"></image>
					</view>
				</view>
				<scroll-view scroll-y="true" class="conest">
					<view class="liso" @click='re(l.reason)' v-for='l in liso'>
						{{l.reason}}
						<image v-if='rea == l.reason' src="/static/img/3/3-75.png" mode="widthFix"></image>
					</view>
				</scroll-view>
				<view class="sup" @click="sup">
					提交
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "",
		components: {
			// iCommentId
		},
		data() {
			return {
				num: 0,
				data: [],
				liso: [],
				rea: '',
				meng: false,
				cOrderNo: '',
				types: '',
				pageIndex: 1
			}
		},
		onLoad(options) {
			let that = this;

			// that.$Request.post(
			// 	that.$api.httpApi.user.getSaleReturnList + '&token=' + that.$SysCache.get('token'), {
			// 		type: "orderclose"
			// 	}
			// ).then(res => {
			// 	if (res.code == 200) {
			// 		that.liso = res.data;
			// 	}
			// });

			if (options.id) {
				this.num = options.id
			}
			if (options.id == 1) {
				that.oder('SUBMITSALERETURN', 1);
				this.types = 'SUBMITSALERETURN'
			} else if (options.id == 2) {
				that.oder('CONFIRMSALERETURNORDER', 1);
				this.types = 'CONFIRMSALERETURNORDER'
			} else if (options.id == 3) {
				that.oder('BUYERRETURN', 1);
				this.types = 'BUYERRETURN'
			} else if (options.id == 4) {
				that.oder('ENDSALERETURN', 1);
				this.types = 'ENDSALERETURN'
			} else if (options.id == 5) {
				that.oder('OPPOSESALERETURN', 1);
				this.types = 'OPPOSESALERETURN'
			} else if (options.id == 6) {
				that.oder('CANCELSALERETURN', 1);
				this.types = 'CANCELSALERETURN'
			} else {
				that.oder(null, 1);
				this.types = null
			}
		},
		onShow() {
			this.onLoad
		},
		methods: {
			sc() {
				let that = this
				let options = {
					id: this.num
				}
				if (options.id == 1) {
					that.oder('SUBMITSALERETURN', 1);
					this.types = 'SUBMITSALERETURN'
				} else if (options.id == 2) {
					that.oder('CONFIRMSALERETURNORDER', 1);
					this.types = 'CONFIRMSALERETURNORDER'
				} else if (options.id == 3) {
					that.oder('BUYERRETURN', 1);
					this.types = 'BUYERRETURN'
				} else if (options.id == 4) {
					that.oder('ENDSALERETURN', 1);
					this.types = 'ENDSALERETURN'
				} else if (options.id == 5) {
					that.oder('OPPOSESALERETURN', 1);
					this.types = 'OPPOSESALERETURN'
				} else if (options.id == 6) {
					that.oder('CANCELSALERETURN', 1);
					this.types = 'CANCELSALERETURN'
				} else {
					that.oder(null, 1);
					this.types = null
				}
			},
			bach(data, id) {
				var that = this;
				var data = {
					dat: data,
					id: id
				}

				uni.navigateTo({
					url: '/pages/user/orderPing?data=' + JSON.stringify(data)
				})


			},
			shou(id) {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '是否确认收到货物！',
					success(ress) {
						if (ress.confirm) {
							that.$Request.post(
								that.$api.httpApi.user.ok + '&token=' + that.$SysCache.get('token'), {
									'cOrderNo': id
								}
							).then(res => {
								if (res.code == 200) {
									uni.showToast({
										title: '确认收货成功！'
									})
									that.oder(that.types);
								}
							});
						}

					}
				})

			},
			oderNew(s) {
				console.log(s);

				var that = this;
				var arr = [];
				// let datas = s
				s.forEach(item => {
					let datas = {
						"iSKUId": item.iSKUId,
						"iProductId": item.iProductId,
						"onLineOffLine": '0',
						"iQuantity": item.iQuantity,
						"dlyType": "PICKUP"
					}

					arr.push(datas)
					// that.$set(item,'onLineOffLine','0')
				})
				let obj = {
					"items": JSON.stringify(arr)
				}

				this.$Request.post(this.$api.httpApi.goods.addCarts + '&token=' + that.$SysCache.get('token'), obj).then(
					res => {
						if (res.code == 200) {
							uni.showToast({
								title: '再次购买成功，已加入购物车！',
								duration: 2000
							})
						}
					})
			},
			invoice(cOrderNo) {
				uni.navigateTo({
					url: '/pages/order/invoice?type=1&cOrderNo=' + cOrderNo
				})
			},
			del(id) {
				let that = this;
				that.$Request.post(
					that.$api.httpApi.user.delOder + '&token=' + that.$SysCache.get('token'), {
						'orderIds': id
					}
				).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: '删除成功！'
						})
						that.oder(that.types);
					}
				});
			},
			sup() {
				let that = this;
				that.$Request.post(
					that.$api.httpApi.user.closeoderOn + '&token=' + that.$SysCache.get('token'), {
						'cOrderNo': that.cOrderNo,
						'reason': that.rea
					}
				).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: '取消成功！'
						})
						that.oder(that.types);
						that.meng = false
					}
				});
			},
			out(cSaleReturnNo) {
				let that = this;

				uni.showModal({
					title: "提示",
					confirmText: "确定",
					content: "取消退款",
					success: function(res) {
						if (res.confirm) {
							that.$Request.post(
								that.$api.httpApi.order.cancelSaleReturn + '&token=' + that.$SysCache.get(
									'token'), {
									"cSaleReturnNo": cSaleReturnNo
								}
							).then(res => {
								if (res.code == 200) {
									uni.showToast({
										title: res.message
									})
								}
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			closes() {
				this.meng = false
			},
			re(rea) {
				this.rea = rea
			},
			tab(id) {
				this.data = []
				this.pageIndex = 1
				var that = this;
				this.num = id;
				if (id == 1) {
					that.oder('SUBMITSALERETURN');
					this.types = 'SUBMITSALERETURN'
				} else if (id == 2) {
					that.oder('CONFIRMSALERETURNORDER');
					this.types = 'CONFIRMSALERETURNORDER'
				} else if (id == 3) {
					that.oder('BUYERRETURN');
					this.types = 'BUYERRETURN'
				} else if (id == 4) {
					that.oder('ENDSALERETURN');
					this.types = 'ENDSALERETURN'
				} else if (id == 5) {
					that.oder('OPPOSESALERETURN');
					this.types = 'OPPOSESALERETURN'
				} else if (id == 6) {
					that.oder('CANCELSALERETURN');
					this.types = 'CANCELSALERETURN'
				} else {
					that.oder(null)
				}
			},
			link(item) {
				uni.navigateTo({
					url: '/pages/user/returnDetailById?cSaleReturnNo=' + item.cSaleReturnNo
				})
			},
			payorder(money, cOrderNo) {
				let that = this
				let paydata = {
					"cOrderNo": cOrderNo,
					"paytype": 1,
					"remark": "营家支付",
					"paytypecode": "construct",
					"payBackUrl": "/client/Pay/payNotify?paytypecode=construct",
					// "trade_type": "JSAPI",
					// "oid": this.$SysCache.get('openid') //openid 
				}
				console.log('uni.getSystemInfoSync().platform', uni.getSystemInfoSync().platform)
				this.$Request.post(this.$api.httpApi.order.buildPayment + "&token=" + this.$SysCache.get('token'), paydata)
					.then(
						res => {
							if (res.code == 200) {
								let typeData = ''
								let openIdData = ''
								let ua = window.navigator.userAgent.toLowerCase()
								// 通过正则表达式匹配ua中是否含有MicroMessenger字符串
								if (ua.match(/MicroMessenger/i) == 'micromessenger') {
									console.log('微信浏览器')
									typeData = "CASHIER_PAY_JSAPI" // 微信浏览器
								} else {
									// alert(uni.getStorageSync('TYPE'))
									if (uni.getStorageSync('TYPE') === '') {
										// alert("CASHIER_PAY_WAP")
										typeData = "CASHIER_PAY_WAP"
									} else {
										// alert("CASHIER_PAY_APP")
										typeData = "CASHIER_PAY_APP"
									}
									// if (uni.getStorageSync('nm').toString() === 'webKit,mobile') {
									// 	typeData = "CASHIER_PAY_WAP"
									// } else if(uni.getStorageSync('nm').toString() === 'webKit,mobile,android') {
									// 	typeData = "CASHIER_PAY_WAP"
									// } else if(uni.getStorageSync('nm').toString() === 'webKit,mobile,ios,android,iPhone,Safari'){
									// 	typeData = "CASHIER_PAY_APP"
									// } else if(uni.getStorageSync('nm').toString() === 'webKit,mobile,android,Safari'){
									// 	typeData = "CASHIER_PAY_APP"
									// } else if(uni.getStorageSync('nm').toString() === 'webKit,mobile,ios,android,iPhone'){
									// 	typeData = "CASHIER_PAY_WAP"
									// }
								}
								console.log('shfw', typeData)
								this.$Request.post4(
									this.$api.httpApi.user.pay, {
										"uid": this.$SysCache.get('userInfo').id || this.$SysCache.get('userInfo').mid,
										"pay_no": res.data.cPayNo,
										"order_no": cOrderNo,
										"token": this.$SysCache.get('token'),

										"page_url": `http://youyou.tangmu.xin/#/pages/order/success?cOrderNo=${cOrderNo}&type=1`,
										// "page_url": `https://vip.inja.com/#/pages/order/success?cOrderNo=${cOrderNo}&type=1`,
										"amount": money,
										"openId": this.$SysCache.get('openid') || null,
										"type": typeData

									}
								).then(res => {
									if (res.code == 1) {
										uni.showLoading({
											title: '正在跳转支付...'
										})
										let ur = this.urlEncode(res.data)
										let xx = ur.substr(0, 1)
										let yy = ur.replace(xx, '?')
										// console.log(yy)
										// window.location.href = "https://ipay.inja.com/gateway/cashier/acquiring?dd=1"+ur  //正式环境
										window.location.href =
											"http://ipay.ysyec.com/gateway/cashier/acquiring?dd=1" + ur //测试环境
										setTimeout(() => {
											uni.hideLoading()
										})

									} else {
										uni.hideLoading()
										uni.showToast({
											title: res.msg,
											icon: 'none'
										})
									}
								})
							}
						})
			},
			urlEncode(param, key, encode) {
				if (param == null) return '';
				var paramStr = '';
				var t = typeof(param);
				if (t == 'string' || t == 'number' || t == 'boolean') {
					paramStr += '&' + key + '=' + ((encode == null || encode) ? encodeURIComponent(param) : param);
				} else {
					for (var i in param) {
						var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
						paramStr += this.urlEncode(param[i], k, encode);
					}
				}
				return paramStr;
			},
			oder(s) {
				let that = this;
				that.$Request.post(
					that.$api.httpApi.user.getSaleReturnList + '&token=' + that.$SysCache.get('token'), {
						'onLineOffLine': 0,
						'orderType': 2,
						status: s,
						pageSize: 10,
						pageIndex: this.pageIndex
					}
				).then(res => {
					if (res.code == 200) {
						// that.data = res.data.list;
						that.data = that.data.concat(res.data.list);
						that.pageIndex++
						if (res.data.list.length == 0) {
							uni.showToast({
								title: "没有更多了！",
								icon: "none"
							})
						}
					}
				});
			},
			modify(item) {
				uni.navigateTo({
					url: '/pages/user/modify?cOrderNo=' + item.cOrderNo + '&cSaleReturnNo=' + item.cSaleReturnNo
				})
			},
		},



	}
</script>

<style scoped>
	.sup {
		width: 90%;
		display: block;
		margin: auto;
		height: 60rpx;
		background: rgba(240, 64, 111, 1);
		color: #fff;
		font-size: 26rpx;
		text-align: center;
		line-height: 60rpx;
		margin-top: 12rpx;
		border-radius: 42rpx;
	}

	.meng {
		position: fixed;
		width: 100%;
		height: 100%;
		background: rgba(0, 0, 0, 0.5);
		top: 0;
		left: 0;
	}

	.tan {
		width: 100%;
		height: 50%;
		background: #fff;
		position: absolute;
		bottom: 0;
		left: 0;
		border-top-right-radius: 5rpx;
		border-top-left-radius: 5rpx;
		padding-left: 20rpx;
		padding-right: 20rpx;
	}

	.tan .titles {
		height: 85rpx;
		line-height: 85rpx;
		font-size: 30rpx;
		border-bottom: 1px solid #dfdfdf;
		position: relative;
	}

	.tan .titles .closes {
		position: absolute;
		width: 85rpx;
		height: 85rpx;
		right: 0;
		text-align: center;
		line-height: 93rpx;
		top: 0;
	}

	.tan .titles image {
		display: inline-block;
		width: 32rpx;


	}

	.tan .conest {
		height: calc(100% - 85rpx - 90rpx);
		overflow: auto;
	}

	.tan .conest .liso {
		height: 90rpx;
		font-size: 26rpx;
		line-height: 90rpx;
		border-bottom: 1px dashed #ededed;
	}

	.tan .conest .liso image {
		display: inline-block;
		float: right;
		width: 28rpx;
		margin-top: 30rpx;
	}

	.butbox {
		text-align: right;
	}

	.butbox view {
		display: inline-block;
		width: 123rpx;
		height: 50rpx;
		font-size: 20rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		border-radius: 25rpx;
		line-height: 50rpx;
		text-align: center;
		vertical-align: middle;
		margin-left: 20rpx;
	}

	.butbox navigator {
		display: inline-block;
		width: 123rpx;
		height: 50rpx;
		font-size: 20rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		border-radius: 25rpx;
		line-height: 50rpx;
		text-align: center;
		vertical-align: middle;
		margin-left: 20rpx;
	}

	.butbox .buta {
		background: rgba(255, 255, 255, 1);
		border: 1px solid rgba(240, 64, 111, 1);
		color: rgba(240, 64, 111, 1);

	}

	.butbox .butb {
		background: rgba(32, 29, 29, 1);
		border-radius: 25px;
		color: rgba(255, 255, 255, 1);
	}

	.both {
		display: block;
		clear: both;
	}

	.con {
		margin-top: 25rpx;
		margin-bottom: 25rpx;
	}

	.numb {
		width: 90rpx;
		text-align: right;
		font-size: 22rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: rgba(102, 102, 102, 1);
		display: inline-block;
		vertical-align: middle;
		margin-left: 68rpx;
	}

	.numb view {
		font-size: 18rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: rgba(153, 153, 153, 1);
		margin-top: 12rpx;
	}

	.con .text {
		display: inline-block;
		vertical-align: middle;
		width: 400rpx;
		font-size: 22rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: rgba(102, 102, 102, 1);
		line-height: 30rpx;
		margin-left: 20rpx;
	}

	.con .text .bu {
		display: block;
		width: 162rpx;
		height: 31rpx;
		background: rgba(240, 64, 111, 1);
		border-radius: 5rpx;
		font-size: 18rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: rgba(255, 255, 255, 1);
		line-height: 30rpx;
		text-align: center;
		margin-top: 15rpx;
	}

	.con .img {
		display: inline-block;
		vertical-align: middle;
		width: 130rpx;
		height: 130rpx;
		background: rgba(255, 255, 255, 1);
		/* border: 1px solid rgba(228, 228, 228, 1); */
		border-radius: 5rpx;
		text-align: center;
		line-height: 130rpx;
	}

	.con .img image {
		display: inline-block;
		width: 100%;
		margin: auto;
		vertical-align: middle;
	}

	.li-up {
		font-size: 22rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: rgba(102, 102, 102, 1);
		margin-bottom: 25rpx;
	}

	.li-up .r {
		float: right;
		color: rgba(240, 64, 111, 1);
	}

	.li-up .ri {
		float: right;
	}

	.ddlist .li {
		background: #fff;
		margin-top: 10rpx;
		padding: 20rpx;
	}

	.top {
		height: 62rpx;
		background: #040000;
		font-size: 26rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: rgba(231, 231, 231, 1);
		text-align: center;
		display: flex;
		line-height: 62rpx;
		overflow-x: auto;
	}

	.top view {
		flex: 1;
		margin-left: 25rpx;
		margin-right: 25rpx;
		white-space: nowrap;
	}

	.active {
		border-bottom: 3rpx solid #F0406F;
		color: #F0406F;
	}

	uni-page-body {
		height: 100%;
		overflow: hidden;
	}

	.mord {
		height: 100%;
		overflow: hidden;
		background: #EEEEEE;
	}

	.ddlist {
		height: calc(100% - 62rpx);
		overflow: auto;
	}
</style>
